define(['moment', 'JDA'], function (moment, JDA) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config', '$calculater',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $calculater) {

            var select_index = -1;
            $scope.query_result = [];

            $scope.init = function(){
                init();
            };

            $scope.back = function(){
                $rootScope.changeProgram('menu', {}, true);
            };

            var total_basis_data_qty = 0;
            $scope.search = function(callback){
                $scope.query_result = [];
                getBasisData(function(report_data){
                    $scope.query_result = report_data;
                    total_basis_data_qty = getTotalMaterialQty();
                    if (callback)
                        callback();
                });
            };

            //長條圖比例css
            $scope.getBarGraphStyle = function(item){
                return {'flex': '0 0 ' + getPercentScale(item.QTY) + '%'};
            };

            // 資料細節開窗
            $scope.showItemDetail = function(item, index){
                if(!item.itemDetail){
                    getReleaseMaterialList(item.MATERIALNO, function(data){
                        item.itemDetail = data||[];
                        var _height = 42 * item.itemDetail.length + 47;
                        item.style = {'flex':'0 0 ' + _height + 'px'};
                    });
                } else {
                    item.itemDetail = undefined;
                    item.style = {'flex':'0 0 0px'};
                }
                /*
                $mdDialog.dialog(
                    'module/moduleMobile/program/mobile09/ItemDetail.tmp.html',
                    function(dialog){
                        return {
                            title: item.MATERIALNO,
                            itemDetail: [],
                            beforeShown: function(){
                                getReleaseMaterialList(item.MATERIALNO, function(data){
                                    dialog.itemDetail = data;
                                });
                            },
                            back : function(){
                                dialog.hide();
                            },
                            confirm : function(){

                            }
                        };
                    }
                );
                */
            };

            function init(){
                $scope.search();
            }

            //取得百分比
            function getPercentScale(value){
                if(total_basis_data_qty > 0)
                    return $calculater.decimalMutiple((+value / +total_basis_data_qty), 100).toPrecision(2);
                else
                    return 0;
            }

            //取得算總物料數量(分母)
            function getTotalMaterialQty(){
                var totalQty = 0;
                $scope.query_result.forEach(function(data){
                    if(!data.style)
                        data.style = {'flex': '0 0 0px'};
                    totalQty = $calculater.decimalAdd(totalQty, data.QTY);
                });
                return totalQty;
            }

            //取得物料發料現況基本資料
            function getBasisData(callback){
                var content = {
                    report_id: 'SR03',
                    query_condition_info: []
                };
                userCustomReportDataGet(content, function(data){
                    $scope.loading_time = moment().format('YYYY-MM-DD HH:mm:ss');
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            // 透過物料發料明細
            function getReleaseMaterialList(material_no, callback){
                var content = {
                    report_id: 'SR0301',
                    query_condition_info: [
                        {column_name: "a.MATERIALNO", query_mode: "0", merge_condition_model: "AND", value: material_no}
                    ]
                };
                userCustomReportDataGet(content, function(data){
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            function userCustomReportDataGet(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_SYS.user_custom_report_data_get',
                    content : content,
                    success : function(data){
                        if(callback){
                            callback(data);
                        }
                    }
                });
            }
        }
    ];
});
